package com.zhouxiaoge.jenkins.core.service.impl;

import com.offbytwo.jenkins.JenkinsServer;
import com.offbytwo.jenkins.model.BuildResult;
import com.offbytwo.jenkins.model.Computer;
import com.offbytwo.jenkins.model.JobWithDetails;
import com.zhouxiaoge.jenkins.core.service.JenkinsManageJobService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.Map;

/**
 * @author 周明阳
 * @date 2019/7/18 09:37
 */

@Slf4j
@Service
public class JenkinsManageJobServiceImpl implements JenkinsManageJobService {

    private final JenkinsServer jenkinsServer;

    public JenkinsManageJobServiceImpl(JenkinsServer jenkinsServer) {
        this.jenkinsServer = jenkinsServer;
    }


    /**
     * 获取Jenkins的Job集合
     *
     * @return Job集合
     */
    @Override
    public Map<String, Computer> acquireJobs() {
        try {
            return jenkinsServer.getComputers();
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }


    /**
     * 创建构建
     */
    @Override
    public void createBuild(String jobName, String jobDescribe) {
        try {
            String jobXml = "<project>\n" +
                    "<actions/>\n" +
                    "<description>" + jobDescribe + "</description>\n" +
                    "<keepDependencies>false</keepDependencies>\n" +
                    "<properties/>\n" +
                    "<scm class=\"hudson.scm.NullSCM\"/>\n" +
                    "<canRoam>true</canRoam>\n" +
                    "<disabled>false</disabled>\n" +
                    "<blockBuildWhenDownstreamBuilding>false</blockBuildWhenDownstreamBuilding>\n" +
                    "<blockBuildWhenUpstreamBuilding>false</blockBuildWhenUpstreamBuilding>\n" +
                    "<triggers/>\n" +
                    "<concurrentBuild>false</concurrentBuild>\n" +
                    "<builders/>\n" +
                    "<publishers/>\n" +
                    "<buildWrappers/>\n" +
                    "</project>";
            jenkinsServer.createJob(jobName, jobXml);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void updateBuildJob(String jobName, String jobDescribe) {

        try {
            String description = jenkinsServer.getJob(jobName).getDescription();
            if (StringUtils.isBlank(jobDescribe)) {
                jobDescribe = description;
            }
            String jobXml = "<project>\n" +
                    "<actions/>\n" +
                    "<description>" + jobDescribe + "</description>\n" +
                    "<keepDependencies>false</keepDependencies>\n" +
                    "<properties/>\n" +
                    "<scm class=\"hudson.scm.NullSCM\"/>\n" +
                    "<canRoam>true</canRoam>\n" +
                    "<disabled>false</disabled>\n" +
                    "<blockBuildWhenDownstreamBuilding>false</blockBuildWhenDownstreamBuilding>\n" +
                    "<blockBuildWhenUpstreamBuilding>false</blockBuildWhenUpstreamBuilding>\n" +
                    "<triggers/>\n" +
                    "<concurrentBuild>false</concurrentBuild>\n" +
                    "<builders>\n" +
                    "<hudson.tasks.BatchFile>\n" +
                    "<command>ipconfig</command>\n" +
                    "</hudson.tasks.BatchFile>\n" +
                    "</builders>\n" +
                    "<publishers/>\n" +
                    "<buildWrappers/>\n" +
                    "</project>";
            jenkinsServer.updateJob(jobName, jobXml);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 执行构建
     */
    @Override
    public void executeBuildJob(String jobName) {
        try {
            jenkinsServer.getJob(jobName).build();
            // 获取执行结果（是否成功）
            BuildResult result = jenkinsServer.getJob(jobName).getLastBuild().details().getResult();
            System.out.println(result.toString());
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    /**
     * 获取备注
     */
    @Override
    public JobWithDetails acquireRemarks(String jobName) {
        try {
            return jenkinsServer.getJob(jobName).details();
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 获取控制台输出
     *
     * @return 控制台输出内容
     */
    @Override
    public String acquireConsoleOutput(String jobName) {
        try {
            //String jobName1 = jenkinsServer.getJob("jobName").getLastBuild().details().getConsoleOutputHtml();

            return jenkinsServer.getJob(jobName).getLastBuild().details().getConsoleOutputText();
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }
}
